package com.cgy.utils.base.reflect;

import static com.cgy.utils.base.constant.CommonConstant.StringSet.EMPTY;
import static com.cgy.utils.base.constant.CommonConstant.StringSet.SEMICOLON;
import static com.cgy.utils.base.constant.Strings.BLANK;

import java.util.Map;

import com.cgy.utils.base.bytes.ByteHelper;
import com.cgy.utils.base.objects.ObjectHelper;
import com.cgy.utils.base.strings.StringHelper;
import com.cgy.utils.base.type.TypeHelper;

import javassist.CtClass;
import javassist.CtField;
import javassist.NotFoundException;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;

/**
 * 字段处理类
 * @author CHTK
 */
public class ReflectFieldHelper {

    /**
     * 获取字段信息
     * @param ctField
     */
    public static FieldInfoContent ctField(final CtField ctField) {
        FieldInfoContent fieldInfoContent = new FieldInfoContent();
        if(null != ctField) {
            //字段名
            String name = ctField.getName();
            fieldInfoContent.setFieldName(name);
            fieldInfoContent.setFieldValue(ByteHelper.toString(ctField.getAttribute(name)));

            //FieldInfo fieldInfo = ctField.getFieldInfo();
        }

        return fieldInfoContent;
    }

    /**
     * 获取字段字符串
     * @return
     */
    public static String makeField(final FieldInfoContent fieldInfoContent) {
        if(StringHelper.isBlank(fieldInfoContent.getFieldName())) {
            return null;
        }

        StringBuilder stringBuilder = new StringBuilder();
        String type = StringHelper.getDefaultValue(fieldInfoContent.getFieldType(), "Object");
        String modify = StringHelper.getDefaultValue(fieldInfoContent.getModify(), "public");

        return stringBuilder.append(modify).append(BLANK).append(type).append(BLANK).append(fieldInfoContent.getFieldName()).append(SEMICOLON).toString();
    }

    /**
     * 获取字段
     * @param obj
     * @param name
     * @return
     */
    public static CtField getField(final Object obj, final String name) {
        CtClass ctClass = ReflectHelper.getCtClass(obj);
        if(null != ctClass) {
            try {
                return ctClass.getDeclaredField(name);
            } catch (NotFoundException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
    /**
     * 获取值
     * @param obj
     * @param name
     * @return
     */
    public static String getString(final Object obj, String name) {
        CtField field = getField(obj, name);
        if(null != field) {
            return new String(field.getAttribute(name));
        }
        return EMPTY;
    }
    /**
     * 获取值
     * @param obj
     * @param name
     * @return
     */
    public static byte[] getValue(final Object obj, String name) {
        CtField field = getField(obj, name);
        if(null != field) {
            return field.getAttribute(name);
        }
        return new byte[0];
    }
    /**
     * 获取值
     * @param obj
     * @param name
     * @return
     */
    public static void setValue(final Object obj, final String name, final Object value) {
        CtField field = getField(obj, name);
        if(null != field) {
            if(TypeHelper.isNumber(value)) {
                byte[] bytes = ByteHelper.getBytes((int) value);
                field.setAttribute(name, bytes);
            } else {
                field.setAttribute(name, ObjectHelper.toString(value).getBytes());
            }
        }
    }


    @Getter
    @Setter
    @Accessors(chain = true)
    public static class FieldInfoContent {
        private String fieldName;
        private String fieldValue;
        private String fieldType;
        private String modify;
        protected Class<?> fieldClass;
        private Map<String, ReflectAnnotationHelper.AnnotationInfoContent> annotationInfoContents;
    }

}
